package com.zmc.product.infrastructure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmc.common.utils.StringUtils;
import com.zmc.common.utils.token.AuthTokenService;
import com.zmc.product.infrastructure.service.CommodityBrandService;
import com.zmc.product.infrastructure.bean.query.CommodityBrandQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.common.utils.PageUtils;

import com.zmc.product.infrastructure.dao.CommodityBrandDao;
import com.zmc.product.infrastructure.entity.CommodityBrandEntity;


/**
 * @author zmc
 */
@Service("commodityBrandService")
public class CommodityBrandServiceImpl extends ServiceImpl<CommodityBrandDao, CommodityBrandEntity> implements CommodityBrandService {

    @Autowired
    private AuthTokenService authTokenService;

    @Override
    public PageUtils pageCommodityBrandList(CommodityBrandQuery query) {
        Page<CommodityBrandEntity> page = baseMapper.selectPage(new Page<>(query.getPageNum(), query.getPageSize()), getWrapper(query));
        return new PageUtils(page);
    }

    @Override
    public List<CommodityBrandEntity> selectCommodityBrandList(CommodityBrandQuery query) {
        return list(getWrapper(query));
    }

    private LambdaQueryWrapper<CommodityBrandEntity> getWrapper(CommodityBrandQuery query) {
        LambdaQueryWrapper<CommodityBrandEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CommodityBrandEntity::getUserId, authTokenService.getUserInfo().getUserId()).
                eq(StringUtils.isNotEmpty(query.getAliasName()), CommodityBrandEntity::getAliasName, query.getAliasName())
                .eq(StringUtils.isNotEmpty(query.getChineseName()), CommodityBrandEntity::getChineseName,query.getChineseName())
                .eq(StringUtils.isNotEmpty(query.getEnglishName()), CommodityBrandEntity::getEnglishName, query.getEnglishName())
                .ge(StringUtils.isNotEmpty(query.getBeginTime()), CommodityBrandEntity::getCreateTime, query.getBeginTime())
                .le(StringUtils.isNotEmpty(query.getEndTime()), CommodityBrandEntity::getCreateTime, query.getEndTime())
                .orderByDesc(CommodityBrandEntity::getCreateTime);
        return wrapper;
    }

}